package com.atshiyou;

import java.util.ArrayList;
import java.util.List;

public class TestThread {
    private ThreadLocal<String> threadLocal = new ThreadLocal<String>();
    private List<String> list =  new ArrayList<String>();
    class A extends Thread{
        @Override
        public void run() {
            System.out.println("A线程开始存值");
            threadLocal.set("thread内容");
            System.out.println("A----->threadLocal="+threadLocal.get());
            list.add("list内容");
        }
    }
    class B extends Thread{
        @Override
        public void run() {
            try {
                Thread.sleep(1000);//让B先休眠，确保A存值成功
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("B线程开始取值");
            System.out.println("threadLocal="+threadLocal.get());
            System.out.println("list="+list.get(0));
        }
    }

    public static void main(String[] args) {
        TestThread testThread = new TestThread();
        TestThread.A a = testThread.new A();
        TestThread.B b = testThread.new B();
        a.start();
        b.start();
    }
}
